<template>
  <el-row class="warp">
    <el-col :span="24" class="warp-breadcrum">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"><b>首页</b></el-breadcrumb-item>
        <el-breadcrumb-item>通讯录管理</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
  
    <el-col :span="24" class="warp-main" v-loading="loading" element-loading-text="拼命加载中">
    
    <el-container>
  		<el-aside style="width:400px;padding-top:20px">
			<el-tree
				  :data="depts"
				  node-key="id"
				  @node-click="handleNodeClick"
				  :default-expanded-keys="[1]"
				  :expand-on-click-node="false">
				<span class="custom-tree-node" style="width:100%;" slot-scope="{ node, data }">
				        <span>{{ node.label }}</span>
				        <span style="float:right">
				        <el-dropdown trigger="click">
						 	<i class="el-icon-more el-dropdown-link"></i>
							<el-dropdown-menu slot="dropdown">
							    <el-dropdown-item @click.native="handleCommand(data,add)">添加同级节点</el-dropdown-item>
							    <el-dropdown-item @click.native="handleCommand(data,edit)">修改</el-dropdown-item>
							    <el-dropdown-item @click.native="handleCommand(data,del)">删除</el-dropdown-item>
							</el-dropdown-menu>
						</el-dropdown>
				        </span>
	     		 </span>
			</el-tree>
  		</el-aside>
 	 	<el-main>
 	 		  <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true">
          <el-form-item>
            <el-button type="primary" @click="showAddDialog">添加</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="setDepts" >批量到处/导入</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="uploadMisuser" >设置所在部门</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="danger" @click="batchRemove">删除</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <!--列表-->
      <el-table :data="users" highlight-current-row v-loading="loading" @selection-change="handleSelectionChange" style="width: 100%;">
      	<el-table-column type="selection" width="50">
      	</el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="name" label="姓名" width="140" sortable>
        </el-table-column>
        <el-table-column prop="position" label="职务" width="220" sortable>
        </el-table-column>
        <el-table-column prop="department" label="部门" width="230" sortable>
        </el-table-column>s
        <el-table-column prop="mobile" label="手机" width="130" min-width="120" sortable>
        </el-table-column>
        <el-table-column prop="email" label="邮箱" width="160" min-width="160" sortable>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button size="mini" @click="showEditDialog(scope.$index,scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="removeUser(scope.$index,scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--工具条-->
      <el-col :span="24" class="toolbar">
        <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total"
                       style="float:right;">
        </el-pagination>
      </el-col>
 	 	</el-main>
	</el-container>
    
      <!-- 添加界面 -->
      <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
        <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
       	  <el-form-item label="姓名" prop="name">
            <el-input v-model="addForm.name" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="别名" prop="alias">
            <el-input v-model="addForm.alias" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="账号" prop="username">
            <el-input v-model="addForm.username" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <template>
				<el-radio v-model="addForm.gender" label="1">男</el-radio>
				<el-radio v-model="addForm.gender" label="0">女</el-radio>
			</template>
          </el-form-item>
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="addForm.mobile"></el-input>
          </el-form-item>
          <el-form-item label="座机" prop="telephone">
            <el-input v-model="addForm.telephone"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input type="email" v-model="addForm.email"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="addForm.address"></el-input>
          </el-form-item>
          <el-form-item label="部门" prop="department">
            <el-input v-model="addForm.department"></el-input>
          </el-form-item>
          <el-form-item label="职务" prop="position">
            <el-input v-model="addForm.position"></el-input>
          </el-form-item>
           <el-form-item label="身份" prop="isLeader">
	           <template>
					<el-radio v-model="addForm.isLeader" label="1">上级</el-radio>
					<el-radio v-model="addForm.isLeader" label="0">普通成员</el-radio>
				</template>
          </el-form-item>
         <!--   <el-form-item label="角色" prop="roleIds">
            <el-checkbox-group v-model="roleIds">
              <el-checkbox v-for="role in roles" :label="role.roleId" :key="role.roleId">{{role.roleName}}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="addFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
        </div>
      </el-dialog>
      <!-- 编辑界面 -->
      <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
        <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
       	  <el-form-item label="姓名" prop="name">
            <el-input v-model="editForm.name" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="别名" prop="alias">
            <el-input v-model="editForm.alias" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="账号" prop="username">
            <el-input v-model="editForm.username" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <template>
				<el-radio v-model="editForm.gender" label="1"">男</el-radio>
				<el-radio v-model="editForm.gender" label="0">女</el-radio>
			</template>
          </el-form-item>
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="editForm.mobile"></el-input>
          </el-form-item>
          <el-form-item label="座机" prop="telephone">
            <el-input  v-model="editForm.telephone"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input type="email" v-model="editForm.email"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="editForm.address"></el-input>
          </el-form-item>
          <el-form-item label="部门" prop="department">
            <el-input  v-model="editForm.department"></el-input>
          </el-form-item>
          <el-form-item label="职务" prop="position">
            <el-input  v-model="editForm.position"></el-input>
          </el-form-item>
           <el-form-item label="身份" prop="isLeader">
            	<template>
					<el-radio v-model="editForm.isLeader" :label="1">上级</el-radio>
					<el-radio v-model="editForm.isLeader" :label="0">普通成员</el-radio>
				</template>
          </el-form-item>
         <!--   <el-form-item label="角色" prop="roleIds">
            <el-checkbox-group v-model="roleIds">
              <el-checkbox v-for="role in roles" :label="role.roleId" :key="role.roleId">{{role.roleName}}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="editFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="editSubmit" :loading="addLoading">提交</el-button>
        </div>
      </el-dialog>
	  <!-- 添加部门界面 -->
      <el-dialog title="添加" :visible.sync="addDeptFormVisible" :close-on-click-modal="false">
        <el-form :model="addDeptForm" label-width="80px" :rules="addDeptFormRules" ref="addDeptForm">
       	  <el-form-item label="上级部门" prop="parentName">
            <el-input v-model="addDeptForm.parentName" readonly auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="部门名字" prop="name">
            <el-input v-model="addDeptForm.name" auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="addDeptFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="addDeptSubmit" :loading="addLoading">提交</el-button>
        </div>
      </el-dialog>
      <!-- 编辑部门界面 -->
      <el-dialog title="编辑" :visible.sync="editDeptFormVisible" :close-on-click-modal="false">
        <el-form :model="editDeptForm" label-width="80px" :rules="editDeptFormRules" ref="editDeptForm">
       	  <el-form-item label="上级部门" prop="parentName">
            <el-input v-model="editDeptForm.parentName" readonly auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="部门名字" prop="name">
            <el-input v-model="editDeptForm.name" auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="editDeptFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="editDeptSubmit" :loading="addLoading">提交</el-button>
        </div>
      </el-dialog>
    </el-col>
  </el-row>
</template>

<script>
  import API from "../../api/api_communication.js";

  export default {
    data() {
      return {
       	depts : [],
       	deptId : '',
        loading: false,
        add:"add",
        del:"del",
        edit:"edit",
        users: [],
        ids:[],
        multipleSelection:[],//多选的数据
        total: 0,
        page: 1,
        limit: 10,
        loading: false,
        addFormVisible: false,//新增界面是否显示
        editFormVisible: false,
        addDeptFormVisible:false,
        editDeptFormVisible: false,
        editFormRules: {
          username: [
            {required: true, message: "请输入账号", trigger: "blur"}
          ],
          name: [
          	{required: true, message: "请输入姓名", trigger: "blur"}
          ]
        },
        editForm: {
          username: "",
          address: "",
          name: "",
          email: "",
          department: "",
          gender:"",
          alias:"",
          telephone:"",
          mobile:"",
          position:"",
          isLeader:""
        },
        //新增相关数据
        addLoading: false,
        addFormRules: {
          username: [
            {required: true, message: "请输入账号", trigger: "blur"}
          ],
          name: [
         	{required: true, message: "请输入姓名", trigger: "blur"}
          ]
        },
        addDeptFormRules:{
        },
        editDeptFormRules:{
        },
        addForm:  {
          username: "",
          address: "",
          name: "",
          email: "",
          department: "",
          gender:"",
          alias: "",
          telephone: "",
          mobile: "",
          position: "",
          isLeader: ""
        },
        addDeptForm:{
        	id:"",
        	name:"",
        	parentId:"",
        	parentName:""
        },
        editDeptForm:{
       		id:"",
        	name:"",
        	parentId:"",
        	parentName:""
        }
      };
    },
    methods: {
      //性别显示转换
      formatSex: function (row, column) {
        return row.gender == 1 ? "男" : row.gender == 0 ? "女" : "未知";
      },
      handleCurrentChange(val) {
        this.page = val;
        this.search(that.deptId);
      },
      handleSelectionChange(val) {
	    this.multipleSelection = val;
	  },
      batchRemove : function(){
      	let that = this;
		const length = this.multipleSelection.length;
		if(length>0){
		 for (let i = 0; i < length; i++) {
		     that.ids.push(that.multipleSelection[i].id)
		 }
		 let params = {
            "ids":that.ids
         }
	      this.$confirm("确认删除该记录吗?", "提示", {type: "warning"})
          .then(() => {
            that.loading = true;
            API.removeUser(params)
              .then(
                function (result) {
                  that.loading = false;
                  if (result > 0  ) {
                    that.$message.success({
                      showClose: true,
                      message: "删除成功",
                      duration: 1500
                    });
                    that.search(that.deptId);
                  }
                },
                function (err) {
                  that.loading = false;
                  that.$message.error({
                    showClose: true,
                    message: err.toString(),
                    duration: 2000
                  });
                }
              )
              .catch(function (error) {
                that.loading = false;
                that.$message.error({
                  showClose: true,
                  message: "请求出现异常",
                  duration: 2000
                });
              });
          })
          .catch(() => {
          });
		}else{
			this.$message.error({
              showClose: true,
              message: "请勾选要删除的人员",
              duration: 2000
            });
		}
		
      },
      setDepts : function(){
      },
      uploadMisuser : function(){
      },
      handleSearch() {
        this.total = 0;
        this.page = 1;
        this.searchDept();
        console.log(this.deptId)
        this.search(this.deptId);
      },
     searchDept: function(){
      	let that = this;
      	let params = {
        };
        that.loading = true;
      	API.findDeptList(params)
          .then(
            function (result) {
              that.loading = false;
              that.depts = [];
              that.depts.push(result)
            },
            function (err) {
              that.loading = false;
              that.$message.error({
                showClose: true,
                message: err.toString(),
                duration: 2000
              });
            }
          )
          .catch(function (error) {
            that.loading = false;
            that.$message.error({
              showClose: true,
              message: "请求出现异常",
              duration: 2000
            });
          });
      },
      //获取部门 ,用户列表
      search: function (deptId) {
        let that = this;
        let params = {
          page: that.page,
          limit: 10,
          deptId:deptId
        };
        that.loading = true;
        API.findList(params)
          .then(
            function (result) {
              that.loading = false;
              if (result && result.rows) {
                that.total = result.total;
                that.users = result.rows;
              }
            },
            function (err) {
              that.loading = false;
              that.$message.error({
                showClose: true,
                message: err.toString(),
                duration: 2000
              });
            }
          )
          .catch(function (error) {
            that.loading = false;
            that.$message.error({
              showClose: true,
              message: "请求出现异常",
              duration: 2000
            });
          });
      },
      showAddDialog: function () {
        let that = this;
        that.addForm.department=that.deptId;
        this.addFormVisible = true;
      },
      showEditDialog: function (index, row) {
        let that = this;
        this.editFormVisible = true;
        this.editForm = Object.assign({}, row);
      },
      handleCommand(data,command) {
          let that = this;
	     if(command=="add"){
	         that.addDeptFormVisible = true;
	         var parentId=data.parentId
	         if(parentId==null || parentId==""){
	         	parentId=1
	         }
	         that.addDeptForm.parentId=parentId;
	         that.addDeptForm.parentName="上海锐道";
	         console.log(that.addDeptForm)
	      }else if(command=="edit"){
	      	 that.editDeptFormVisible = true;
	      	 that.editDeptForm.parentName="上海锐道";
	      	 that.editDeptForm.parentId=data.parentId;
	      	 that.editDeptForm.id=data.id;
	      	 that.editDeptForm.name=data.label;
	      }else if(command=="del"){
			  let params = {
		            "id":data.id
		      }
	      	  this.$confirm("确认删除该记录吗?", "提示", {type: "warning"}).then(() => {
	      	 		that.loading = true;
		            API.delDept(params)
		              .then(
		                function (result) {
		                  that.loading = false;
		                  if (result >0 ) {
		                    that.$message.success({
		                      showClose: true,
		                      message: "删除成功",
		                      duration: 1500
		                    });
		                    that.searchDept();
		                    that.deptId=""
		                    that.search(that.deptId);
		                  }
		                },
		                function (err) {
		                  that.loading = false;
		                  that.$message.error({
		                    showClose: true,
		                    message: err.toString(),
		                    duration: 2000
		                  });
		                }
		              )
		              .catch(function (error) {
		                that.loading = false;
		                that.$message.error({
		                  showClose: true,
		                  message: "请求出现异常1",
		                  duration: 2000
		                });
		             });
	      	  })
	      	  .catch(() => {
          	  });
	      }
      },
      addDeptSubmit:function(){
      	let that = this;
      	 this.$refs.addDeptForm.validate(valid => {
      	 	 if (valid) {
      	 	 	that.loading = true;
      	 	 	let params = Object.assign({}, this.addDeptForm);
      	 	 	API.addDept(params).then(function (result) {
	      	 	 	 if (1 === result) {
			            that.loading = false;
			            that.$message;
			            that.$message.success({
			               showClose: true,
			               message: "新增成功",
			               duration: 2000
			            });
			            that.$refs["addDeptForm"].resetFields();
			            that.addDeptFormVisible = false;
			            that.searchDept();
	              	} else {
		                that.$message.error({
		                  showClose: true,
		                  message: "新增失败",
		                  duration: 2000
		                });
	              	}
      	 	 	});
      	 	 }
      	 });
      },
      editDeptSubmit : function(){
      	let that = this;
      	 this.$refs.editDeptForm.validate(valid => {
	      	 if (valid) {
	      	 	that.loading = true;
	      	 	let params = Object.assign({}, this.editDeptForm);
	      	 	API.editDept(params).then(function (result) {
	      	 		 if (1 === result) {
			            that.loading = false;
			            that.$message;
			            that.$message.success({
			               showClose: true,
			               message: "修改成功",
			               duration: 2000
			            });
			            that.$refs["editDeptForm"].resetFields();
			            that.editDeptFormVisible = false;
			            that.searchDept();
	              	} else {
		                that.$message.error({
		                  showClose: true,
		                  message: "修改失败",
		                  duration: 2000
		                });
	              	}
	      	 	});
	      	 }
      	 });
      },
      addSubmit: function () {
        let that = this;
        this.$refs.addForm.validate(valid => {
          if (valid) {
            that.loading = true;
            let params = Object.assign({}, this.addForm);
            API.addUser(params).then(function (result) {
              if (1 === result) {
                that.loading = false;
                that.$message;
                that.$message.success({
                  showClose: true,
                  message: "新增成功",
                  duration: 2000
                });
                that.$refs["addForm"].resetFields();
                that.addFormVisible = false;
                that.search(that.deptId);
              } else {
                that.$message.error({
                  showClose: true,
                  message: "新增失败",
                  duration: 2000
                });
              }
            });
          }
        });
      },
      editSubmit: function () {
        let that = this;
        this.$refs.editForm.validate(valid => {
          if (valid) {
            that.loading = true;
            let params = Object.assign({}, that.editForm);
            API.editUser(params).then(function (result) {
              if (1 === result) {
                that.$message.success({
                  showClose: true,
                  message: "修改成功",
                  duration: 2000
                });
                that.$refs["editForm"].resetFields();
                that.editFormVisible = false;
                that.search(that.deptId);
              } else {
                that.$message.error({
                  showClose: true,
                  message: "修改失败",
                  duration: 2000
                });
              }
            });
          }
        });
      },
      removeUser: function (index, row) {
        let that = this;
      	let id = row.id;
      	that.ids.push(id);
      	let params = {
            "ids":that.ids
        }
        this.$confirm("确认删除该记录吗?", "提示", {type: "warning"})
          .then(() => {
            that.loading = true;
            API.removeUser(params)
              .then(
                function (result) {
                  that.loading = false;
                  if (result >0 ) {
                    that.$message.success({
                      showClose: true,
                      message: "删除成功",
                      duration: 1500
                    });
                    that.search(that.deptId);
                  }
                },
                function (err) {
                  that.loading = false;
                  that.$message.error({
                    showClose: true,
                    message: err.toString(),
                    duration: 2000
                  });
                }
              )
              .catch(function (error) {
                that.loading = false;
                that.$message.error({
                  showClose: true,
                  message: "请求出现异常2",
                  duration: 2000
                });
              });
          })
          .catch(() => {
          });
      },
      handleNodeClick: function (data) {
      		let that=this
      		that.deptId = data.id;
      		that.search(that.deptId);
      }
    }, 
    mounted() {
      this.handleSearch();
    }
  };
</script>

<style>

</style>
